package com.fjzx.androidframe.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fjzx.androidframe.base.activity.BaseSimpleActivity;
import com.fjzx.androidframe.base.annotation.injector.ContentViewInjector;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 基础Fragment类，不带数据请求器
 *
 * @author xiongyongcong
 */
public abstract class BaseSimpleFragment extends Fragment {
    protected BaseSimpleActivity mActivity;
    protected Unbinder mUnbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = bindContentView(inflater, container, savedInstanceState);
        mUnbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        initView(view);
    }

    protected View bindContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return ContentViewInjector.inject(this, inflater, container);
    }

    @Override
    @CallSuper
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity = (BaseSimpleActivity) context;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
        this.mActivity = null;
    }

    protected void showToast(String msg) {
        mActivity.showToast(msg);
    }

    protected void showProgress(String msg) {
        mActivity.showProgress(msg);
    }

    protected void showUnCancelProgress(String msg) {
        mActivity.showUnCancelProgress(msg);
    }

    protected void hideProgress() {
        mActivity.hideProgress();
    }

    /**
     * 子类重写该方法，初始化View
     *
     * @param view 根布局View
     */
    abstract protected void initView(View view);
}
